package com.nwpu.am.controller.common;

import com.nwpu.am.constant.ErrorConstant;
import com.nwpu.am.pojo.dto.PartAddDTO;
import com.nwpu.am.pojo.dto.PartDeleteDTO;
import com.nwpu.am.pojo.dto.PartFindDTO;
import com.nwpu.am.pojo.dto.PartUpdateDTO;
import com.nwpu.am.pojo.entity.Part;
import com.nwpu.am.pojo.entity.Warehouse;
import com.nwpu.am.result.PageResult;
import com.nwpu.am.result.Result;
import com.nwpu.am.service.PartService;
import com.nwpu.am.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * PartController
 *
 * @author Du
 * @version 1.0
 * {@code @date} 2024/6/7
 */

@RestController
@RequestMapping("/part")
@Slf4j
@Api(tags = "零件通用接口")
public class PartController {
    @Resource
    private PartService partService;

    @Resource
    private WarehouseService warehouseService;

    @GetMapping("/selectPartPage")
    @ApiOperation("展示零件")
    public Result<PageResult<Part>> page(PartFindDTO partFindDTO) {
        log.info("零件分页查询，参数为：{}", partFindDTO);
        PageResult<Part> partPageResult = partService.pageQuery(partFindDTO);
        return Result.success(partPageResult);
    }

    @GetMapping("/updatePart")
    @ApiOperation("更新零件状态")
    public Result<Part> updatePart(@RequestBody @Validated PartUpdateDTO partUpdateDTO) {
        Warehouse warehouse = warehouseService.findWarehouseByName(partUpdateDTO.getWarehouseName());
        if(warehouse == null){
            return Result.error(ErrorConstant.WAREHOUSE_NOT_FIND);
        }

        Part part = partService.findPartByNameId(partUpdateDTO.getPartName(), warehouse.getWarehouseId());
        if (part == null){
            return Result.error(ErrorConstant.PART_NOT_FIND);
        }

        int num = part.getPartNum() + partUpdateDTO.getFluctuation();
        if (num < 0 ){
            return Result.error(ErrorConstant.QUANTITY_SHORTAGE);
        }
        partUpdateDTO.setWarehouseId(part.getWarehouseId());

        return Result.success(partService.updatePart(partUpdateDTO));
    }

    @PostMapping("/addPart")
    @ApiOperation("添加零件")
    public Result<Part> addPart(@RequestBody @Validated PartAddDTO partAddDTO) {
        Warehouse warehouse = warehouseService.findWarehouseByName(partAddDTO.getWarehouseName());
        if(warehouse == null){
            return Result.error(ErrorConstant.WAREHOUSE_NOT_FIND);
        }

        Part part = partService.findPartByNameId(partAddDTO.getPartName(), warehouse.getWarehouseId());
        if (part != null){
            return Result.error(ErrorConstant.PART_HAS_EXISTED);
        }
        partAddDTO.setWarehouseId(warehouse.getWarehouseId());

        return Result.success(partService.addPart(partAddDTO));
    }


    @DeleteMapping("/deletePart")
    @ApiOperation("删除零件")
    public Result<String> deletePart(@RequestBody @Validated PartDeleteDTO partDeleteDTO) {
        Warehouse warehouse = warehouseService.findWarehouseByName(partDeleteDTO.getWarehouseName());
        if(warehouse == null){
            return Result.error(ErrorConstant.WAREHOUSE_NOT_FIND);
        }

        Part part = partService.findPartByNameId(partDeleteDTO.getPartName(), warehouse.getWarehouseId());
        if (part != null){
            return Result.error(ErrorConstant.PART_HAS_EXISTED);
        }
        partDeleteDTO.setWarehouseId(warehouse.getWarehouseId());
        partService.deletePart(partDeleteDTO);
        return Result.success();
    }
}
